package com.zhouxiaoge.kryo;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周小哥 gqzmy@163.com 2022/8/23 16:37
 */

public class KryoMain {
    public static void main(String[] args) throws FileNotFoundException {
        Map<String, Node> nodeMap = new HashMap<>();

        StartNode startNode = new StartNode();
        startNode.setAction("加法");
        startNode.setId(1L);
        startNode.setName("startNode");

        EndNode endNode = new EndNode();
        endNode.setEndName("结束节点");

        Map<String, String> map = new HashMap<>();
        map.put("name", "小哥");
        map.put("age", "18");
        endNode.setMap(map);

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("a");
        list.add("中");
        endNode.setList(list);

        endNode.setId(2L);
        endNode.setName("endNode");


        nodeMap.put("startNode", startNode);
        nodeMap.put("endNode", endNode);
        Kryo kryo = new Kryo();
        kryo.setRegistrationRequired(false);

        kryo.register(HashMap.class);
        kryo.register(StartNode.class);
        kryo.register(EndNode.class);
        kryo.register(Node.class);
        kryo.register(ArrayList.class);
        Output output = new Output(new FileOutputStream("file.bin"));
        kryo.writeObject(output, nodeMap);
        output.close();

        Input input = new Input(new FileInputStream("file.bin"));
        Map map1 = kryo.readObject(input, HashMap.class);
        System.out.println(map1);
    }
}
